﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using RoadTeam.DAL;
using System.Data;
using System.Reflection;
using RoadTeam.Enums;
using RoadTeam.Model;

public partial class 映射枚举值_GetEnumsNameValu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        NameValueCollection value = GetEnmun("UnitColumnFiled");

        for (int i = 0; i < value.Count; i++)
        {
            Response.Write("<p>键" + value.GetKey(i) + "值:" + value.GetValues(value.GetKey(i))[0] + "</p>");
        }
    }



    protected void Button2_Click(object sender, EventArgs e)
    {
        #region 测试数据(单元数据)
        //DataTable dt = GetRtRmUnit();
        //DataTable Syncdt = GetRmUnit();
        //String Rtidentity = @"UnitIdentifier";
        //String idtntity = @"UnitIdentifier";
        //String EnmunType = @"UnitColumnFiled";
        #endregion
        DataTable dt = GetRtRmUnit();
        DataTable Syncdt = GetRmUnit();
        String Rtidentity = @"UnitIdentifier";
        String idtntity = @"UnitIdentifier";
        String EnmunType = @"UnitColumnFiled";

        #region 同步前数据
        GridView3.DataSource = dt;
        GridView3.DataBind();
        GridView1.DataSource = Syncdt;
        GridView1.DataBind();
        #endregion

        SyncDataTable(dt, Syncdt, Rtidentity, idtntity, EnmunType);

        #region 同步后数据
        GridView2.DataSource = Syncdt;
        GridView2.DataBind();
        #endregion
        #region 每一行状态
        String Str = String.Empty;
        foreach (DataRow dr in Syncdt.Rows)
        {
            Str += @"<p>" + dr.RowState.ToString() + "</p>";
        }
        Label1.Text = Str;
        #endregion

    }

    public void SyncDataTable(DataTable dt, DataTable Syncdt, String Rtidentity, String idtntity, String EnmunType)
    {
        NameValueCollection value = GetEnmun(EnmunType);
        DataRow[] drs;
        DataRow drs_Temp;
        String RtColumnName = String.Empty;
        String ColumnName = String.Empty;

        List<String> LiRtColumnName = new List<string>();
        List<string> LiColumnName = new List<string>();

        foreach (DataColumn col in dt.Columns)
            LiRtColumnName.Add(col.ColumnName);
        foreach (DataColumn col in Syncdt.Columns)
            LiColumnName.Add(col.ColumnName);
        foreach (DataRow dr in dt.Rows)
        {
            drs = Syncdt.Select(idtntity + "='" + dr[Rtidentity] + "'");
            if (drs.Count() == 0) continue;
            for (int i = 0; i < value.Count; i++)
            {
                //对比所有对应的字段,如果没有相同值
                if (value.GetKey(i) == Rtidentity || value.GetValues(value.GetKey(i))[0] == idtntity) continue;
                drs_Temp = drs[0];
                RtColumnName = value.GetKey(i);
                ColumnName = value.GetValues(value.GetKey(i))[0];

                if (!LiRtColumnName.Contains(RtColumnName) || !LiColumnName.Contains(ColumnName)) continue;

                if (drs_Temp[ColumnName] == dr[RtColumnName]) continue;

                drs_Temp[ColumnName] = dr[RtColumnName];
            }
        }
    }

    private NameValueCollection GetEnmun(String EnmunType)
    {
        NameValueCollection value = new NameValueCollection();
        Assembly assemly = Assembly.LoadFrom(Server.MapPath("~/Bin/RoadTeam.Enums.dll"));
        String ClassName = (GlobalConfig.RtStemType.ToUpper() == "CS") ? "Cs" + EnmunType : "Bs" + EnmunType;
        Type[] T = assemly.GetTypes();
        object obj = null;
        object returnobj = null;
        foreach (Type t in T)
        {
            if (t.Name == ClassName)
            {
                obj = Activator.CreateInstance(t);
                foreach (PropertyInfo pi in t.GetProperties())
                {
                    returnobj = pi.GetValue(obj, null);
                }
                break;
            }
        }
        value = (NameValueCollection)returnobj;
        return value;
    }



    #region 获取机构数据
       public DataTable GetRtRmUnit()
    {
        String StrSql = @"SELECT UnitID,
UnitCode,
UnitName,
UnitFullName,
UnitIdentifier,
TbRmBuilding.OrganID as OrganID,
TbRmBuilding.BuildingName,
TbRmPavilion.PavilionName,
TbRmUnit.[Floor]
FROM TbRmBuilding 
INNER JOIN TbRmPavilion on TbRmBuilding.BuildingID=TbRmPavilion.BuildingID
INNER JOIN  TbRmUnit on TbRmUnit.PavilionID=TbRmPavilion.PavilionID
";

        DataTable dt = DbFactory.CreateInstance().CreateDb().ExecuteStrQuery(StrSql, CommandType.Text, GlobalConfig.RtConnectionString);
        return dt;
    }
    public DataTable GetRmUnit()
    {
        SqlWhere where = new SqlWhere();
        return RoadTeam.Model.RmUnit.Select(where, GlobalConfig.ConnectionString);
    }
    #endregion
    protected void Button3_Click(object sender, EventArgs e)
    {
        DataTable dt = GetRtRmUnit();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}